import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const $time 	= $Lazy.file(()=>import('@com/utils/time'))

const isData = [{name: "是",value: '1'},{name: "否", value: '2'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '部门',		name: 'dept_id',	type: 'select', loading:true, idStr:'uuid', nameStr:'depart_name' },
			{ label: '岗位',		name: 'uuid',		type: 'select', loading:true, idStr:'uuid' },
			{ label: '状态',		name: 'active',		type: 'select', loading:true },
			{ label: '员工姓名',		name: 'real_name' },
			{ label: '员工编码',		name: 'empl_name' },
			{ label: '邮箱编码',		name: 'email' },
			{ label: '手机号码',		name: 'phone' },
		],
		// 提交表单列表
		formData : [
			{ label: '员工编号',		name: 'empl_name', 		full:true, required:true },
			{ label: '真实姓名',		name: 'real_name', 		half:true, required:true },
			{ label: '英文名称',		name: 'en_name', 		half:true },
			{ label: '密码',		name: 'password', 		half:true, required:true, type: 'password' },
			{ label: '确认密码',		name: 'confirm_password', half:true, required:true, type: 'password' },
			{ label: '手机号码',		name: 'phone', 			half:true, required:true },
			{ label: '身份证',		name: 'idcard', 		half:true, required:true },
			{ label: '性别',		name: 'sex', 			half:true, required:true, type:'select', idStr:'code'},
			{ label: '电子邮件',		name: 'email', 			half:true, required:true },
			{ label: '年龄',		name: 'age', 			half:true, required:true, type:'number'  },
			{ label: '所属部门',		name: 'dept_id', 		half:true, required:true, type:'select', idStr:'uuid', nameStr:'depart_name' },
			{ label: '所属科室',		name: 'pgroup_ids', 	half:true, type:'select', mode:'multiple', idStr:'uuid', onChange:({ value })=> this.getJobs(value)},
			{ label: '所属岗位',		name: 'jobs', 			half:true, type:'select', mode:'multiple', p: '请先选择科室', idStr:'uuid' },
			{ label: '职位权限',		name: 'position', 		half:true, type:'select', mode:'multiple', idStr:'uuid'  },
			{ label: '角色权限',		name: 'roles', 			half:true, type:'select', mode:'multiple', idStr:'uuid', nameStr:'role_name'},
			{ label: '分组权限',		name: 'groups', 		half:true, type:'select', mode:'multiple', idStr:'uuid' },
			{ label: '入职时间',		name: 'entrytime', 		half:true, type:'date-time', format:1 },
			{ label: '头像',		name: 'head_portrait', 	full:true, type:'upload', code:121 },
			{ label: '电子签名',		name: 'signature', 		half:true, type:'upload', code:121, param: () => this.signatureParam },		 // 上传电子签名添加source_come,uuid参数 2021-9-7  --马婷 
			{ label: '共享签名',		name: 'share_signature', half:true, type:'checkbox', disabled:true },
			{ label: '描述',		name: 'empl_desc', 		full:true, type:'textarea' }
		]
	}
	
	model = {}
	signatureParam = {source_come: 'signature'}
	componentDidMount(){
		// 获取上级页面参数
		this.uuid = $fn.query('uuid')
		if(this.uuid){ this.query = { uuid: this.uuid } }
		//
		this.fetch()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 部门
			$fn.setRows([ forms, formData ], 'dept_id', await f.getCache({ api:'department' } ) )
			// 岗位
			$fn.setRow(forms, 'uuid', await f.getCache({ api:'job' } ) )
			// 回显岗位
			if(this.uuid){
				this.searchFormRef.setValue({ uuid: this.uuid  })
			}
			// 状态
			const stateData = $fn.setRow(forms, 'active', await f.employee_state )
			this.setState({ forms, stateData })
		})
	}
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, ['sex','pgroup_ids','position','roles','groups'], ()=>{
			$cache.then( async f => {
				// 性别
				$fn.setRow(formData, 'sex', await f.getDicCache({ code: 45700 } ) )
				// 所属科室
				$fn.setRow(formData, 'pgroup_ids', await f.getCache({ api: 'ke_shi' } ) )
				// 职位权限
				$fn.setRow(formData, 'position', await f.getCache({ api: 'position' } ) )
				// 角色权限
				$fn.setRow(formData, 'roles', await f.getCache({ api: 'role' } ) )
				// 分组权限
				$fn.setRow(formData, 'groups', await f.getCache({ api: 'group' } ) )
				// 
				this.setState({ formData })
			})
		})
	}
	// 获取岗位
	getJobs = value => {
		window.$http.submit(null,'project-team/jobselect', { param: { uuid: value }, isPull:true }).then(data=>{
			const { formData } = this.state
			if($fn.hasArray(data)){
				$cache.then( async f => {
					// 所属岗位
					$fn.setRow(formData, 'jobs', data )
					$fn.getRow(formData,'jobs').p = '请选择所属岗位'
					//
					this.setState({ formData })
				})
			}
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'employee/index')
	
	// 表单
	cols = [
		{ title: '员工编码',		dataIndex: 'empl_name', 		width:100 },
		{ title: '员工名称', 	dataIndex: 'real_name', 		...$fn.table.name },
		{ title: '手机号码', 	dataIndex: 'phone', 			...$fn.table.phone },
		{ title: '邮箱地址', 	dataIndex: 'email', 			width:200 },
		{ title: '性别', 		dataIndex: 'sex_name', 			...$fn.table.sex },
		{ title: '状态', 		dataIndex: 'active', 			...$fn.table.status, render: text => $fn.getState(this.state.stateData, text)},
		{ title: '本司员工', 	dataIndex: 'is_corp', 			...$fn.table.name,render: text => $fn.getState(isData, text) },
		{ title: '所属公司', 	dataIndex: 'company_name', 		width:150 },
		{ title: '所属部门', 	dataIndex: 'depart_name', 		width:150 },
		{ title: '所属科室', 	dataIndex: 'project_name', 		width:100 },
		TableControls.controls.call(this, { width:300, align:'left'})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, active }) => {
		const { formData } = this.state
		return [
			// 编辑
			TableControls.edit.call(this, 'employee/info', { uuid }, { ids:({ comp_id })=>({ comp_id }), click:()=>{
				this.signatureParam.uuid = uuid
				$fn.disabledRows(formData, 'empl_name', true)
				$fn.getRow(formData,'password').required = false
				$fn.getRow(formData,'confirm_password').required = false
			}, select: this.getModalSelect, load: async ({ pgroup_ids, signature })=>{
				this.formRef.setValue(await this.getJobs(pgroup_ids))
				if(signature){
					$fn.disabledRows(formData, 'share_signature', false)
				}else{
					$fn.disabledRows(formData, 'share_signature', true)
				}
				this.setState({formData})
			} }),			
			
			(+active === 2 || +active === 0) && this.onEnabled( 1,  [ uuid ] ),		// 启用
			(+active === 1  || +active === 0) && this.onEnabled( 0,  uuid ),		// 禁用
			
			TableControls.pushTo.call(this, '权限关联', `permissions/?route=employee&uuid=${uuid}`),
		]
	}
	// 操作按钮
	controls = () => [
		// 添加
		Controls.add.call(this, { 
			click: ()=>{
				const { formData } = this.state
				$fn.disabledRows(formData, 'empl_name', false)
				$fn.getRow(formData,'password').required = true
				$fn.getRow(formData,'confirm_password').required = true
				//设置默认时间为当天
				$time.then(f=>{
					this.formRef.setValue({ entrytime: f.getNow() })
				})
				this.setState({ formData })
				this.getModalSelect()
				this.submit = {is_corp:2,active:1}
			},
			
		}),			
		this.onEnabled(1),					// 启用
		Controls.imports.call(this,'employee/import', { query:{}, op_key: 'EmployeeInfoExcel', reconfirm: true }),		// 文件导入
		// 下载导入模板
		Controls.importTemplate.call(this),
		this.uuid && Controls.back.call(this)
	]
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'employee/open', off:'employee/delete', isKeys: !uuid })

	render(){
		return (
			<>
				<Page title='员工管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ width:'80%', labelWidth:'s7', api: 'employee/add', editApi:'employee/edit', scroll:true, onRefresh:()=>{
					$fn.dataRemove('employee/select')
				} }) }
			</>
		)
	}
}

export default Index